package dt.inlee.system.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import dt.inlee.common.exception.FrameworkException;
import dt.inlee.common.util.BaseUtil;
import dt.inlee.common.util.SystemResourceUtil;
import dt.inlee.domain.BusinessDictionary;
import dt.inlee.domain.BusinessDictionaryDetail;
import dt.inlee.system.dao.BusinessDictionaryDao;
import dt.inlee.system.vo.BusinessDictionaryDetailBean;

/**
 * @功能说明：业务字典业务层
 * @作者：雷运梅、潘瑞峥
 * @创建日期：2010-6-4
 */
@Service("businessDictionaryService")
public class BusinessDictionaryService {
	@Resource(name = "businessDictionaryDao")
	private BusinessDictionaryDao businessDictionaryDao;
	@Resource(name = "businessDictionaryDetailService")
	private BusinessDictionaryDetailService businessDictionaryDetailService;

	/**
	 * 通过业务字典ID获取业务字典明细
	 * @return
	 * @throws FrameworkException
	 */
	@Transactional
	public List<BusinessDictionaryDetail> findBusinessDictionaryDetailsByBusiId(String id) throws FrameworkException {
		return this.businessDictionaryDao.findBusinessDictionaryDetailsByBusiId(id);
	}

	/**
	 * 获取所有的业务字典实体
	 * @return
	 * @throws FrameworkException
	 */
	@SuppressWarnings("unchecked")
	public List<BusinessDictionary> findAllBusinessDictionaries() throws FrameworkException {
		return this.businessDictionaryDao.findAll(BusinessDictionary.class);
	}

	/**
	 * 通过业务字典ID删除业务字典实体
	 * 
	 * @param businessDictionaryId
	 * @throws FrameworkException
	 */
	@Transactional
	public void deleteBusinessDictionaryById(String businessDictionaryId) throws FrameworkException {
		this.businessDictionaryDao.deleteById(BusinessDictionary.class, businessDictionaryId);
		SystemResourceUtil.getInstance().loadBusinessDictionary();
	}

	/**
	 * 根据条件获取业务字典参数总记录数
	 * @param cnName
	 * @param enName
	 * @return
	 * @throws FrameworkException
	 */
	public int getBusinessDictionaryTotalRows(String cnName, String enName) throws FrameworkException {
		return this.businessDictionaryDao.getBusinessDictionaryTotalRows(cnName, enName);
	}

	/**
	 *  根据条件获取业务字典参数记录
	 * @param cnName
	 * @param enName
	 * @param curPage
	 * @param pageSize
	 * @return
	 * @throws FrameworkException
	 */
	public List<BusinessDictionary> findAllBusinessDictionaries(String cnName, String enName, int curPage, int pageSize)
			throws FrameworkException {
		return this.businessDictionaryDao.findAllBusinessDictionaries(cnName, enName, curPage, pageSize);
	}

	/**
	 * 通过业务字典ID获取业务字典实体
	 * 
	 * @param businessDictionaryId
	 * @return
	 * @throws FrameworkException
	 */
	public BusinessDictionary findBusinessDictionaryById(String businessDictionaryId) throws FrameworkException {
		return (BusinessDictionary) this.businessDictionaryDao.get(BusinessDictionary.class, businessDictionaryId);
	}

	/**
	 * 保存业务字典
	 * 
	 * @param businessDictionary
	 * @throws FrameworkException
	 */
	public void save(BusinessDictionary businessDictionary) throws FrameworkException {
		businessDictionaryDao.save(businessDictionary);
	}

	/**
	 * 修改业务字典
	 * 
	 * @param businessDictionary
	 * @throws FrameworkException
	 */
	@Transactional
	public void updateBusinessDictionary(BusinessDictionary businessDictionary) throws FrameworkException {
		this.businessDictionaryDao.update(businessDictionary);
	}

	/**
	 * 通过业务字典英文名称和中文名称查找业务字典
	 * (如果为空，表示可使用；不为空，表示已经存在此名称)
	 * 
	 * @param enName
	 * @param cnName
	 * @return
	 * @throws FrameworkException
	 */
	public BusinessDictionary findBusinessDictionaryByName(String enName, String cnName) throws FrameworkException {
		return this.businessDictionaryDao.findBusinessDictionaryByName(enName, cnName);
	}

	/**
	 * 通过业务字典ID、英文名称和中文名称查找业务字典
	 * 检索除此id外已有的名称
	 * (如果为空，表示可使用；不为空，表示已经存在此名称)
	 * 
	 * @param id
	 * @param enName
	 * @param cnName
	 * @return
	 * @throws FrameworkException 
	 */
	public BusinessDictionary findBusinessDictionaryByIdAndName(String id, String enName, String cnName)
			throws FrameworkException {
		return this.businessDictionaryDao.findBusinessDictionaryByIdAndName(id, enName, cnName);
	}

	/**
	 * 新增业务字典及其字典明细
	 * 
	 * @param businessDictionary
	 * @param list
	 * @throws FrameworkException
	 */
	@Transactional
	public void saveBusinessDictionary(BusinessDictionary businessDictionary, List<BusinessDictionaryDetail> list)
			throws FrameworkException {
		if (!BaseUtil.isEmpty(list)) {
			List<BusinessDictionaryDetail> businessDictionaryDetails = new ArrayList<BusinessDictionaryDetail>();
			for (BusinessDictionaryDetail businessDictionaryDetail : list) {
				businessDictionaryDetail.setBusinessDictionary(businessDictionary);
				businessDictionaryDetails.add(businessDictionaryDetail);
			}
			businessDictionary.setBusinessDictionaryDetails(businessDictionaryDetails);
		}
		this.save(businessDictionary);
		SystemResourceUtil.getInstance().loadBusinessDictionary();
	}

	/**
	 * 修改业务字典及其字典明细
	 * 
	 * @param bd
	 * @param insertVal
	 * @param updateVal
	 * @param deleteVal
	 * @throws FrameworkException
	 * @throws Exception
	 */
	@Transactional
	public void updateBusinessDictionaryDetail(BusinessDictionary bd, String insertVal, String updateVal,
			String deleteVal) throws FrameworkException, Exception {
		// 业务字典明细list
		List<BusinessDictionaryDetail> businessDictionaryDetails = new ArrayList<BusinessDictionaryDetail>();
		// 给业务字典赋新值
		BusinessDictionary businessDictionary = this.findBusinessDictionaryById(bd.getId());
		businessDictionary.setCnName(bd.getCnName());
		businessDictionary.setEnName(bd.getEnName());
		businessDictionary.setFlag(bd.getFlag());
		businessDictionary.setRemark(bd.getRemark());
		// 保存业务字典
		this.updateBusinessDictionary(businessDictionary);

		// 新增明细
		if (!"".equals(insertVal) && null != insertVal) {
			for (String inVal : insertVal.split(",")) {
				if (!"".equals(inVal) && null != inVal) {
					String[] val = inVal.split(";");
					// 明细name、value、remark
					String name = null;
					String value = null;
					String remark = null;
					if (!"".equals(val[0]) && null != val[0]) {
						name = val[0].trim();
					}
					if (!"".equals(val[1]) && null != val[1]) {
						value = val[1].trim();
					}
					// 明细remark存在
					if (3 == val.length) {
						if (!"".equals(val[2]) && null != val[2]) {
							remark = val[2].trim();
						}
					}
					// 给明细赋值
					BusinessDictionaryDetail businessDictionaryDetail = new BusinessDictionaryDetail();
					businessDictionaryDetail.setName(name);
					businessDictionaryDetail.setValue(value);
					businessDictionaryDetail.setRemark(remark);
					businessDictionaryDetail.setBusinessDictionary(businessDictionary);
					businessDictionaryDetails.add(businessDictionaryDetail);
					businessDictionary.setBusinessDictionaryDetails(businessDictionaryDetails);
					this.updateBusinessDictionary(businessDictionary);
				}
			}
		}

		// 修改明细
		if (!"".equals(updateVal) && null != updateVal) {
			for (String upVal : updateVal.split(",")) {
				if (!"".equals(upVal) && null != upVal) {
					String[] val = upVal.split(";");
					// 明细id、name、value、remark
					String id = null;
					String name = null;
					String value = null;
					String remark = null;
					if (!"".equals(val[0]) && null != val[0]) {
						id = val[0].trim();
					}
					if (!"".equals(val[1]) && null != val[1]) {
						name = val[1].trim();
					}
					if (!"".equals(val[2]) && null != val[2]) {
						value = val[2].trim();
					}
					// 明细remark存在
					if (4 == val.length) {
						if (!"".equals(val[3]) && null != val[3]) {
							remark = val[3].trim();
						}
					}
					// 给明细赋值
					BusinessDictionaryDetail businessDictionaryDetail = businessDictionaryDetailService
							.findBusinessDictionaryDetailById(id);
					businessDictionaryDetail.setName(name);
					businessDictionaryDetail.setValue(value);
					businessDictionaryDetail.setRemark(remark);
					businessDictionaryDetail.setBusinessDictionary(businessDictionary);
					businessDictionaryDetails.add(businessDictionaryDetail);
					businessDictionary.setBusinessDictionaryDetails(businessDictionaryDetails);
					this.updateBusinessDictionary(businessDictionary);
				}
			}
		}

		// 删除明细
		if (!"".equals(deleteVal) && null != deleteVal) {
			String id = deleteVal.trim();
			for (String delVal : deleteVal.split(",")) {
				if (!"".equals(delVal) && null != delVal) {
					id = delVal.trim();
					BusinessDictionaryDetail businessDictionaryDetail = businessDictionaryDetailService
							.findBusinessDictionaryDetailById(id);
					businessDictionary.getBusinessDictionaryDetails().remove(businessDictionaryDetail);
					businessDictionaryDetailService.deleteById(id);
				}
			}
		}
		SystemResourceUtil.getInstance().loadBusinessDictionary();
	}

	/**
	 * 通过业务字典ID删除业务字典实体
	 * 
	 * @param businessDictionaryId
	 * @throws FrameworkException
	 */
	@Transactional
	public void deleteBusDictionaryByIds(String businessDictionaryIds) throws FrameworkException {
		String[] ids =  businessDictionaryIds.split(",");
		for (String id : ids) {
			this.businessDictionaryDao.deleteById(BusinessDictionary.class, id);
		}
		SystemResourceUtil.getInstance().loadBusinessDictionary();
	}
	
	/**
	 * 根据id删除字典子明细
	 * 
	 * @param businessDictionary
	 * @param listDelete
	 * @throws FrameworkException
	 */
	@Transactional
	public void deleteBusDictionaryDetailByIds(
			BusinessDictionary businessDictionary,
			List<BusinessDictionaryDetailBean> listDelete)
			throws FrameworkException {
		for (BusinessDictionaryDetailBean businessDictionaryDetailBean : listDelete) {
			BusinessDictionaryDetail businessDictionaryDetail = businessDictionaryDetailService
					.findBusinessDictionaryDetailById(businessDictionaryDetailBean
							.getId());
			businessDictionary.getBusinessDictionaryDetails().remove(
					businessDictionaryDetail);
			businessDictionaryDetailService
					.deleteById(businessDictionaryDetailBean.getId());
		}

	}
	
	/**
	 * 根据id修改字典子明细
	 * 
	 * @param businessDictionary
	 * @param listUpdated
	 * @throws FrameworkException
	 */
	@Transactional
	public void updateBusDictionaryDetailByIds(
			BusinessDictionary bd,
			List<BusinessDictionaryDetailBean> listUpdated)
			throws FrameworkException {
		BusinessDictionaryDetail businessDictionaryDetail = null;
		//给业务字典赋新值
		BusinessDictionary businessDictionary = this.findBusinessDictionaryById(bd.getId());
		// 业务字典明细list
		List<BusinessDictionaryDetail> businessDictionaryDetails = new ArrayList<BusinessDictionaryDetail>();
		for (BusinessDictionaryDetailBean businessDictionaryDetailBean : listUpdated) {
			// 给明细赋值
			businessDictionaryDetail = businessDictionaryDetailService
					.findBusinessDictionaryDetailById(businessDictionaryDetailBean.getId());
			businessDictionaryDetail.setName(businessDictionaryDetailBean.getName());
			businessDictionaryDetail.setValue(businessDictionaryDetailBean.getValue());
			businessDictionaryDetail.setRemark(businessDictionaryDetailBean.getRemark());
			businessDictionaryDetail.setBusinessDictionary(businessDictionary);
			businessDictionaryDetails.add(businessDictionaryDetail);
		}
		businessDictionary.setBusinessDictionaryDetails(businessDictionaryDetails);
		this.updateBusinessDictionary(businessDictionary);
	}

	/**
	 * 根据id修改字典子明细
	 * 
	 * @param businessDictionary
	 * @param listInserted
	 * @throws FrameworkException
	 * @throws FrameworkException
	 */
	@Transactional
	public void insertBusDictionaryDetailByIds(
			BusinessDictionary bd,
			List<BusinessDictionaryDetailBean> listInserted)
			throws FrameworkException {
		// 给明细赋值
		BusinessDictionaryDetail businessDictionaryDetail = null;
		//给业务字典赋新值
		BusinessDictionary businessDictionary = this.findBusinessDictionaryById(bd.getId());
		// 业务字典明细list
		List<BusinessDictionaryDetail> businessDictionaryDetails = new ArrayList<BusinessDictionaryDetail>();
		for (BusinessDictionaryDetailBean bean : listInserted) {
			businessDictionaryDetail = new BusinessDictionaryDetail();
			businessDictionaryDetail.setName(bean.getName());
			businessDictionaryDetail.setValue(bean.getValue());
			businessDictionaryDetail.setRemark(bean.getRemark());
			businessDictionaryDetail.setBusinessDictionary(businessDictionary);
			businessDictionaryDetails.add(businessDictionaryDetail);
			businessDictionary
					.setBusinessDictionaryDetails(businessDictionaryDetails);
			this.updateBusinessDictionary(businessDictionary);
		}
	}
}